package com.wl156.activities;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import com.wl156.R;
import com.wl156.utils.AddressStringUtil;
import com.wl156.widget.ProviceSelector;

public class MapAndonActivity extends MapActivity implements OnClickListener {

	private static final String TAG = null;
	private MapView mMapView;
	private ProviceSelector mFromProviceSelector;
	private ProviceSelector mToProviceSelector;
	// private EditText mFrom;
	// private EditText mTo;
	private TextView mDistance;
	private Button mConfirm;
	double[] addr1;
	double[] addr2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		Window win = getWindow();
//		win.requestFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.act_two_city);
//		win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
//				R.drawable.ic_launcher);

		mMapView = (MapView) findViewById(R.id.mapview);
		// mFrom = (EditText) findViewById(R.id.from);
		// mTo = (EditText) findViewById(R.id.to);
		mFromProviceSelector = (ProviceSelector) findViewById(R.id.provice_selector);
		mFromProviceSelector.setTitle("起点: ");
		mFromProviceSelector.setProviceRequried(true);
		mFromProviceSelector.setCityRequried(true);
		// mFromProviceSelector.hideRegion(true);
		mToProviceSelector = (ProviceSelector) findViewById(R.id.to_provice_selector);
		mToProviceSelector.setTitle("终点: ");
		mToProviceSelector.setProviceRequried(true);
		mToProviceSelector.setCityRequried(true);
		// mToProviceSelector.hideRegion(true);
		mConfirm = (Button) findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		mDistance = (TextView) findViewById(R.id.distance);

		// 设置地图的缩放级别。这个值的取值范围是[1,21],
		// mMapView.getController().setZoom(15);
	}

	private String getFromText() {
		final String startLocationLv1 = mFromProviceSelector.getProvice();
		final String startLocationLv2 = mFromProviceSelector.getCity();
		final String startLocationLv3 = mFromProviceSelector.getRegion();

		return startLocationLv1 + startLocationLv2 + startLocationLv3;
	}

	private String getToText() {
		final String endLocationLv1 = mToProviceSelector.getProvice();
		final String endLocationLv2 = mToProviceSelector.getCity();
		final String endLocationLv3 = mToProviceSelector.getRegion();

		return endLocationLv1 + endLocationLv2 + endLocationLv3;
	}

	private void showLocation() {
		try {
			new AsyncTask<Void, Void, Void>() {

				protected void onPostExecute(Void v) {
					try {
						float[] result = new float[3];
						Location.distanceBetween(addr1[0], addr1[1], addr2[0],
								addr2[1], result);
						System.out.println("distanceBetween: " + result[0]);
						mDistance.setText(String
								.format("距离: %.3f 米", result[0]));

						Drawable marker = getResources().getDrawable(
								R.drawable.map_marker);
						marker.setBounds(0, 0, marker.getIntrinsicWidth(),
								marker.getIntrinsicHeight());
						PlaceOverlay overlay = new PlaceOverlay(marker, "");
						mMapView.getOverlays().add(overlay);

						GeoPoint pt = overlay.getCenter(); // get the
															// first-ranked
															// point
						mMapView.getController().setCenter(pt);
					} catch (Exception e) {
					}
				};

				@Override
				protected Void doInBackground(Void... params) {
					addr1 = AddressStringUtil.getLocationInfo(getFromText());
					addr2 = AddressStringUtil.getLocationInfo(getToText());
					return null;
				}
			}.execute();
		} catch (Exception e) {
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	class PlaceOverlay extends ItemizedOverlay<OverlayItem> {

		GeoPoint gp;
		Drawable defaultMarker;
		String name;

		public PlaceOverlay(Drawable defaultMarker, String name) {
			super(defaultMarker);
			this.defaultMarker = defaultMarker;
			this.name = name;

			populate();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			try {
				Projection projection = mapView.getProjection();

				for (int i = 0; i < size(); i++) {
					OverlayItem overLayItem = getItem(i); // 得到给定索引的item
					String title = overLayItem.getTitle();
					// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
					Point point = projection.toPixels(overLayItem.getPoint(),
							null);
					Paint paintText = new Paint();
					paintText.setColor(Color.RED);
					paintText.setAntiAlias(true);
					paintText.setFakeBoldText(true);
					paintText.setTextSize(20);
					canvas.drawText(title,
							point.x - paintText.measureText(title) / 2, point.y
									- defaultMarker.getIntrinsicHeight() - 5,
							paintText); // 绘制文本

					super.draw(canvas, mapView, shadow);
					boundCenterBottom(defaultMarker);
				}
			} catch (Exception e) {
			}
		}

		@Override
		protected OverlayItem createItem(int i) {
			double[] addr;
			String name;
			if (i == 0) {
				name = getFromText();

				addr = addr1;
			} else {
				name = getToText();

				addr = addr2;
			}
			double latitude = addr[0];
			double longtitude = addr[1];

			Log.d(TAG, "latitude: " + latitude + ", longtitude: " + longtitude);
			gp = new GeoPoint((int) (Double.valueOf(latitude) * 1E6),
					(int) (Double.valueOf(longtitude) * 1E6));
			return new OverlayItem(gp, name, "snippet");
		}

		@Override
		public int size() {
			return 2;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			if (TextUtils.isEmpty(getFromText())
					|| TextUtils.isEmpty(getToText())) {
				Toast.makeText(this, "请输入起点和终点", Toast.LENGTH_SHORT).show();
				return;
			}
			showLocation();
			break;
		}
	}
}